package com.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


import com.ysh.gsm.CallMessage;
import com.ysh.gsm.Callleg;

public class DetailedDecoder {

	native public byte[] DecodeMsgInCallLeg(String fileName, int offset,
			int size);

	static {
		System.loadLibrary("detaildecode");
		System.loadLibrary("libBtsmDecoder");
		System.loadLibrary("libRrDecoder");

	}
	private static int MSGID = 4;
	private static int DIRECTION = 1;
	private static int PROTOCOL = 1;
	private static int TIME_LEN = 4;
	private static int TIME_EXT_LEN = 2;
	private static int NAME_LEN = 64;
	private static int BIN_LEN = 2;
	private static int DE_LEN = 2;

//	public static void main(String[] args) {
//		String fileName = "D:\\JNI\\a2.abis";
//		// /72527
//		byte[] ret = new DetailedDecoder().DecodeMsgInCallLeg(fileName, 0, 300);
//		for (int j = 0; j < ret.length; j++) {
//			System.out.format("%02X ", ret[j]);
//		}
//		System.out.println();
//	}

	private byte[] deCode(String fileName, int offset, int size) {
		return new DetailedDecoder().DecodeMsgInCallLeg(fileName, offset, size);
	}

	private String bytes2HexString(byte[] b) {
		String ret = "";
		for (int i = 0; i < b.length; i++) {
			String hex = Integer.toHexString(b[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			ret += hex.toUpperCase();
		}
		return ret;
	}

	public List<CallMessage> getAllMesgs(Callleg leg) {
		byte[] b = deCode(leg.getFileName(), leg.getFileOffset(),
				leg.getFileObjectSize());
		int index = 0;
		int len = b.length;
		List<CallMessage> messages = new ArrayList<CallMessage>();
		String dtStr, extStr;
		while (index + 80 <= len) {
			CallMessage cm = new CallMessage(leg.getBscId(), leg.getCellId());
			index += MSGID;
			dtStr = bytes2HexString(Arrays.copyOfRange(b, index, TIME_LEN
					+ index));
			index += TIME_LEN;
			cm.setDirection(b[index]);
			index += DIRECTION;
			cm.setProtocol(b[index]);
			index += PROTOCOL;
			extStr = bytes2HexString(Arrays.copyOfRange(b, index, TIME_EXT_LEN
					+ index));
			cm.setDateTime(new Date(Integer.parseInt(dtStr, 16) * 1000L
					+ Short.parseShort(extStr, 16)));
			index += TIME_EXT_LEN;
			cm.setName(getMesgName(Arrays.copyOfRange(b, index, NAME_LEN
					+ index)));
			index += NAME_LEN;
			extStr = bytes2HexString(Arrays.copyOfRange(b, index, BIN_LEN
					+ index));
			cm.setBinCodeLen(Short.parseShort(extStr, 16));
			index += BIN_LEN;
			extStr = bytes2HexString(Arrays.copyOfRange(b, index, DE_LEN
					+ index));
			cm.setDeCodeLen(Short.parseShort(extStr, 16));
			index += DE_LEN;
			cm.setBinCode((Arrays.copyOfRange(b, index, cm.getBinCodeLen()
					+ index)));
			index += cm.getBinCodeLen();
			cm.setDetailCode(getMesgName(Arrays.copyOfRange(b, index, cm.getDeCodeLen()
					+ index)));
			index += cm.getDeCodeLen();
			messages.add(cm);
			//System.out.println(cm.getDetailCode());
		}

		return messages;
	}
	

	private String getMesgName(byte[] b) {
		int i = 0;
		for (byte by : b) {
			if (by == 0) {
				break;
			}
			i++;
		}
		return new String(Arrays.copyOfRange(b, 0, i));

	}

}
